from django.db import models

# Create your models here.


class ParsedLog(models.Model):
    time = models.DateTimeField("时间标签")
    host = models.CharField(max_length=128, verbose_name="主机名")
    severity = models.CharField(max_length=32, verbose_name="严重等级")
    facility = models.CharField(max_length=32)
    process = models.CharField(max_length=128, verbose_name="子系统")
    message = models.TextField("消息")
    log_processing = models.ForeignKey('LogProcessing', models.CASCADE, null=True, blank=True)

    class Meta:
        verbose_name = "原始日志记录"
        verbose_name_plural = "原始日志记录表"

    @property
    def dup_fields(self):
        """用于比较进行去重的字段"""
        return ['host', 'severity', 'facility', 'process', 'message']

    @property
    def dup_tuple(self):
        """用于去重的所有字段的值
        必须是元组，元组是不可变的，可以作为字典的key
        """
        return self.host, self.severity, self.facility, self.process, self.message

    def __str__(self):
        return "%s %s" % (self.process, self.message)


class LogProcessing(models.Model):
    first_log = models.ForeignKey('ParsedLog', models.CASCADE, verbose_name="首次出现", related_name='first_log')
    last_log = models.ForeignKey('ParsedLog', models.CASCADE, verbose_name="最后出现", related_name='last_log')
    count = models.PositiveIntegerField("重复次数")
    handling_status = models.CharField(max_length=32, verbose_name="处理状态", default='未处理')
    auto_tag = models.CharField(max_length=32, verbose_name="自动标签", blank=True, null=True)
    c_time = models.DateField("创建日期", auto_now_add=True)

    # 添加一些额外的属性，方便使用
    def get_first_log_time(self):
        return self.first_log.time
    get_first_log_time.short_description = "首次出现"  # 由于有这么一句，就不能使用@property
    first_log_time = property(get_first_log_time)  # 效果同装饰器@property

    def get_last_log_time(self):
        return self.last_log.time
    get_last_log_time.short_description = "末次出现"
    last_log_time = property(get_last_log_time)

    def get_last_log_host(self):
        return self.last_log.host
    get_last_log_host.short_description = "主机名"
    host = property(get_last_log_host)

    def get_last_log_severity(self):
        return self.last_log.severity
    get_last_log_severity.short_description = "严重等级"
    severity = property(get_last_log_severity)

    def get_last_log_facility(self):
        return self.last_log.facility
    facility = property(get_last_log_facility)

    def get_last_log_process(self):
        return self.last_log.process
    get_last_log_process.short_description = "子系统"
    process = property(get_last_log_process)

    def get_last_log_message(self):
        return self.last_log.message
    get_last_log_message.short_description = "消息"
    message = property(get_last_log_message)

    class Meta:
        verbose_name = "日志处理记录"
        verbose_name_plural = "日志处理记录表"

    def __str__(self):
        return "%s(%s)" % (self.last_log, self.count)


class AutoAuditRule(models.Model):
    name = models.CharField(max_length=128, verbose_name="描述名称", unique=True)
    key = models.TextField("匹配字符串",
                           help_text="正则的特殊字符需要转义：\"*.?+$^[](){}|\/\"，"
                                     "模板+正则不支持这3个字符\"${}\"")
    type_choices = (
        (1, "完全匹配"),
        (2, "包含"),
        (3, "正则匹配"),
        (4, "正则+模板"),
    )
    type = models.SmallIntegerField('匹配类型', choices=type_choices)
    count = models.PositiveSmallIntegerField("数量要求", default=0)
    auto_tag = models.CharField(max_length=32, verbose_name="自动标签")
    comments = models.TextField("说明")
    c_time = models.DateTimeField("创建时间", auto_now_add=True)

    class Meta:
        verbose_name = "自动审计规则"
        verbose_name_plural = "自动审计规则表"
        unique_together = (('key', 'count'),)

    def __str__(self):
        return "%s: %s" % (self.name, self.auto_tag)




